//
//  ZPSelectCityVC.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/2/6.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPSelectCityVC.h"
#import "ZPRegSelectCityAndGradeCell.h"
#import "ZPRegSelectGrade.h"
#import "ZPRegSelectCity.h"

@interface ZPSelectCityVC () <UITableViewDelegate,UITableViewDataSource>

@property (copy, nonatomic) NSMutableArray *citys;

@property (weak, nonatomic) IBOutlet UITableView *cityTableView;

@end

@implementation ZPSelectCityVC

- (NSMutableArray *)citys
{
    if (_citys == nil) {
        _citys = [NSMutableArray array];
    }
    return _citys;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getCitysList];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = @"选择城市";
    [self.navigationController setNavigationBarHidden:NO];
    [self.tabBarController.tabBar setHidden:YES];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(clickBackButtonItem)];
}


- (void)clickBackButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - <获取年级和城市>
- (void)getCitysList
{
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"user/getSelect.json" parameters:@{} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue]== 200) {
            if ([responseObject valueForKey:@"result"]) {
                // 返回城市
                NSArray *resultCitys  = [ZPRegSelectCity mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"city"]];
                if (resultCitys.count) {
                    [self.citys addObjectsFromArray:resultCitys];
                }
            }
        }
        // 重载
        [self.cityTableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        
        [ZPPromptView showMessage:@"请检查您的网络设置"];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.citys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const reuseIdentifier  = @"ZPRegSelectCityAndGradeCell";
    ZPRegSelectCityAndGradeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPRegSelectCityAndGradeCell class]) owner:self options:nil] lastObject];
    }
    ZPRegSelectCity *city = self.citys[indexPath.row];
    cell.selectLab.text = city.name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.valueBlock1([self.citys[indexPath.row] name],[self.citys[indexPath.row] selectId]);
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
